package com.study.concurrent.A_ThreadManagement.b_GetThreadInfoAndSettings;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by 赵超 on 2017/3/14.
 */
public class Main {
    public static void main(String[] args) {
        /**
         * 存储线程的数组
         */
        Thread[] threads = new Thread[10];
        /**
         * 存储线程信息的数组
         */
        Thread.State[] status = new Thread.State[10];
        /**
         * 创建一个容量为10的Calculator对象数组，为每个对象设置不同的数字，
         * 用它们来创建10个线程，5个的优先级设置为最高，其余5个最低
         */
        for (int i = 0; i < 10; i++) {
            threads[i] = new Thread(new Calculator(i));
            if ((i % 2 == 0)) {
                threads[i].setPriority(Thread.MAX_PRIORITY);
            } else {
                threads[i].setPriority(Thread.MIN_PRIORITY);
            }
            threads[i].setName("Thread_" + i);
        }

        PrintWriter printWriter = null;
        try {
            /**
             * 创建一个PrintWriter对象，用来把线程的状态演变写入到文件中
             */
            FileWriter fileWriter = new FileWriter("log.txt");
            printWriter = new PrintWriter(fileWriter);
            /**
             * 把这10个线程的状态写入文件中，现在线程的状态是NEW
             */
            for (int i = 0; i < 10; i++) {
                printWriter.println("Main：Status of Thread " + i + "：" + threads[i].getState());
                status[i] = threads[i].getState();
            }
            /**
             * 开始执行10个线程
             */
            for (int i = 0; i < 10; i++) {
                threads[i].start();
            }
            /**
             * 查看线程状态是否改变，改变了就会记录
             */
            boolean finish = false;
            while (!finish) {
                for (int i = 0; i < threads.length; i++) {
                    if (threads[i].getState() != status[i]) {
                        writeThreadInfo(printWriter, threads[i], status[i]);
                        status[i] = threads[i].getState();
                    }
                }
            }
            finish = true;
            /**
             * 判断线程是否死亡，只要存在未死亡的线程就会继续对线程进行监控
             */
            for (int i = 0; i < threads.length; i++) {
                finish = finish && (threads[i].getState() == Thread.State.TERMINATED);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (printWriter != null)
                printWriter.close();
        }
    }

    /**
     * 将线程的ID、名称、优先级、新旧状态写入文件
     * @param pw
     * @param thread    线程对象
     * @param state     线程之前的状态（旧状态）
     */
    private static void writeThreadInfo(PrintWriter pw, Thread thread, Thread.State state) {
        pw.printf("Main : Id %d - %s\n", thread.getId(), thread.getName());
        pw.printf("Main : Properties %d\n", thread.getPriority());
        pw.printf("Main : Old State %s\n", state);
        pw.printf("Main : New State %s\n", thread.getState());
        pw.printf("Main : ****************************************\n");
        pw.flush();
    }
}
